** This Do-File replicates Tables 1, 2, 3 and Figure 3 from P. Dutronc, A. Tondini "Large Means-Tested Pensions with Informal Labor Markets" ** 

set more off

cd "" /*Input here directory where the PALMS v3.2 (only years from 2002--2015) is stored */ 

use "south_africa_palms.dta", clear

**************************************
** Select Population for Estimation ** 
**************************************

drop if year==2008 | year==2009 /* Exclude years in which the threshold is lowered from 65 to 60 */ 

keep if pop_group==1 | pop_group==2  /* Keep Black and Coloured individuals only, see text for explanation */ 

drop if sex==. | sex==9 /* Exclude observations with missing value for gender */ 

gen married=(marital_status==1) /* 1/0 variable for marital status */ 


keep if age>=55 & age<65 /*Window +/- 5 */ 

** Adjust weights for multiple year estimation ** 

bys year: egen mean_weight=mean(weight) 
replace weight=weight/mean_weight /* Adjust weights to have the same mean across waves */

** Definition of Informal Employment **

gen tot_inf=1 if status==1 & no_written_contract==1 /*Employees w/o written contract */
replace tot_inf=0 if status==1 & no_written_contract==0  /*Employees w. written contract */
replace tot_inf=1 if status==2 & not_registered==1 /*Self-employed w/o registered business*/
replace tot_inf=0 if status==2 & not_registered==0 /*Self-employed w. registered business*/
replace tot_inf=0 if status==3 | status==4 /*Not Employed*/ 

keep if tot_inf!=. /* Exclude from the estimation those with missing informality status.*/

gen f_emp=(tot_inf==0 & emp==1)


**  Wages and Hours ** 

replace hrs_wrk=0 if status==3 | status==4 /* Set hours equal to zero if not employed */
replace monthly_sal=0 if status==3 | status==4 /* Set monthly salary equal to zero if not employed */

gen hrs_wrk_f=hrs_wrk if f_emp==1 /* hours of work in formal employment */
replace hrs_wrk_f=0 if f_emp==0 /* hours of work in formal employment */

gen hourly_wage=monthly_sal/(hrs_wrk*4.5)

replace monthly_sal=ln(monthly_sal) 
replace hourly_wage=ln(hourly_wage)


drop if hourly_wage==. & emp==1 /* Exclude if salary or hours information is missing */ 


**************************************
************ Regressions ** **********
**************************************
gen OAP_1=(age>=60) /* Indicator variable for being at or above the threshold */ 


gen x=age-60 /* center discontinuity at 0 */ 
gen x2=x^2 

gen post=(year>2008) 

gen yob=year-age  

egen cluster=group(yob pop_group) /* Cluster for standard errors by race*cohort group to account for repeated observations over time in DiDisc estimation, see text for explanation */ 




************
**Table 1 **
************

**********************************************************************************

local f_form x /*Linear Functional Form */ 
*local controls i.Province i.married i.education /* set of control variables */ 

** Males ** 

reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==0, robust
outreg2 using table1_male.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==0, robust
outreg2 using table1_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==0, robust
outreg2 using table1_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==1, robust
outreg2 using table1_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==1, robust
outreg2 using table1_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==1 & post==1, robust
outreg2 using table1_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)

table post [aw=weight] if age==59 & sex==1, c(mean emp mean tot_inf mean f_emp)

** Females ** 

reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==0, robust
outreg2 using table1_female.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==0, robust
outreg2 using table1_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1  `controls' [w=weight] if sex==2 & post==0, robust
outreg2 using table1_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==1, robust
outreg2 using table1_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==1, robust
outreg2 using table1_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 1.OAP_1 `controls'  [w=weight] if sex==2 & post==1, robust
outreg2 using table1_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1)

table post [aw=weight] if age==59 & sex==2, c(mean emp mean tot_inf mean f_emp)

**********************************************************************************

erase table1_male.tex 
erase table1_female.tex
erase table1_male.txt 
erase table1_female.txt

************
**Table 2 **
************

**********************************************************************************

local f_form x /*Linear Functional Form */ 
*local controls i.Province i.married i.education /* set of control variables */ 

** Males ** 

reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using table2_male.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using table2_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using table2_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)
reg hrs_wrk_f  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using table2_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)

table post [aw=weight] if age==59 & sex==1, c(mean emp mean tot_inf mean f_emp mean hrs_wrk_f)


** Females ** 

reg emp i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using table2_female.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg tot_inf i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using table2_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg f_emp i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using table2_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)
reg hrs_wrk_f i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using table2_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)

table post [aw=weight] if age==59 & sex==2, c(mean emp mean tot_inf mean f_emp mean hrs_wrk_f)

**********************************************************************************


erase table2_male.tex 
erase table2_female.tex
erase table2_male.txt 
erase table2_female.txt



************
**Table 3 **
************
**********************************************************************************

local f_form x /*Linear Functional Form */ 
*local controls i.Province i.married i.education /* set of control variables */ 

gen self=(tot_inf==1 & status==2) /* Binary variable for informal self-employed */ 
gen wage_earner=(tot_inf==1 & status==1) /* Binary variable for informal wage-earner */ 

reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using table3_male.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg wage_earner  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using table3_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg self  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using table3_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 

table post [aw=weight] if age==59 & sex==1, c(mean tot_inf mean wage_earner mean self )

****************************************************************************************

erase table3_male.tex 
erase table3_male.txt 



****************************************************************************************

collapse emp tot_inf f_emp [w=weight], by(age sex year post)  /* Average within-year (with sample weights) */
collapse emp tot_inf f_emp, by(age sex  post)  /* Average across years */


************
**Figure 3 **
************

** Panel A -men ** 
****************************************************************************************

preserve 
keep if sex==1

twoway (lfit emp age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit emp age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit emp age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit emp age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter emp age if post==0, mcolor(black) msize(small)) /// 
|| (scatter emp age if post==1, msymbol(square_hollow ) mcolor(black) msize(small)) ///
||, ytitle("Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) ///
||, legend(order(5 6) label(5 "Before") label(6 "After"))  xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.6)
restore 


preserve 
keep if sex==1
twoway (lfit f_emp age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit f_emp age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit f_emp age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit f_emp age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter f_emp age if post==0, mcolor(black) msize(small)) /// 
|| (scatter f_emp age if post==1, msymbol(square_hollow) mcolor(black) msize(small)) ///
||, ytitle("Formal Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) ///
||, legend(order(5 6) label(5 "Before") label(6 "After")) xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.6)
restore 



preserve 
keep if sex==1
twoway (lfit tot_inf age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit tot_inf age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit tot_inf age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit tot_inf age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter tot_inf age if post==0, mcolor(black) msize(small)) /// 
|| (scatter tot_inf age if post==1, msymbol(square_hollow) mcolor(black) msize(small)) ///
||, ytitle("Informal Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) /// 
||, legend(order(5 6) label(5 "Before") label(6 "After"))  xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.3)
restore 


****************************************************************************************


** Panel B - women ** 
****************************************************************************************

preserve 
keep if sex==2

twoway (lfit emp age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit emp age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit emp age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit emp age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter emp age if post==0, mcolor(black) msize(small)) /// 
|| (scatter emp age if post==1, msymbol(square_hollow ) mcolor(black) msize(small)) ///
||, ytitle("Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) ///
||, legend(order(5 6) label(5 "Before") label(6 "After"))  xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.6)
restore 


preserve 
keep if sex==2
twoway (lfit f_emp age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit f_emp age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit f_emp age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit f_emp age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter f_emp age if post==0, mcolor(black) msize(small)) /// 
|| (scatter f_emp age if post==1, msymbol(square_hollow) mcolor(black) msize(small)) ///
||, ytitle("Formal Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) ///
||, legend(order(5 6) label(5 "Before") label(6 "After")) xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.6)
restore 


preserve 
keep if sex==2
twoway (lfit tot_inf age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit tot_inf age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit tot_inf age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit tot_inf age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter tot_inf age if post==0, mcolor(black) msize(small)) /// 
|| (scatter tot_inf age if post==1, msymbol(square_hollow) mcolor(black) msize(small)) ///
||, ytitle("Informal Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) /// 
||, legend(order(5 6) label(5 "Before") label(6 "After"))  xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.3)
restore 

****************************************************************************************
